﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace ModernShopping.AppCode
{
    public static class DbCompany
    {
        public static bool CreateNewAccount(string Username, string Password, Company Company)
        {
            try
            {
                MembershipUser User = Membership.CreateUser(Username, Password, Company.Email);
                Guid userID = (Guid)User.ProviderUserKey;
                Company.Code = userID;
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                db.Companies.InsertOnSubmit(Company);
                db.SubmitChanges();

                Roles.AddUserToRole(Username, "User");
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Company getCompany(Guid Code)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            return db.Companies.Where(c => c.Code.Equals(Code)).FirstOrDefault();
        }

        public static Company getCompany(string Username = null, string Email = null)
        {
            Guid Code;
            try
            {
                if (Username != null)
                {
                    Code = (Guid)Membership.GetUser(Username).ProviderUserKey;
                }
                else
                {
                    Code = (Guid)Membership.GetUser(Membership.GetUserNameByEmail(Email)).ProviderUserKey;
                }
                return getCompany(Code);
            }
            catch
            {
                return null;
            }
        }


        public static bool UpdateLogo(string Username, string NewLogo)
        {
            MembershipUser user = Membership.GetUser(Username);
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            Company company = db.Companies.Where(c => c.Code.Equals(user.ProviderUserKey)).FirstOrDefault();
            if (company != null)
            {
                company.Logo = NewLogo;
                db.SubmitChanges();
                return true;
            }
            return false;
        }

        public static string GetLogoURL(string Username)
        {
            MembershipUser user = Membership.GetUser(Username);
            return GetLogoURL((Guid)user.ProviderUserKey);
        }
        public static string GetLogoURL(Guid UserID)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            Company company = db.Companies.Where(c => c.Code.Equals(UserID)).FirstOrDefault();
            if (company != null)
            {
                return company.Logo;
            }
            return null;
        }

        public static bool ChangePassword(Guid UserID, string OldPassword, string NewPassword)
        {
            MembershipUser User = Membership.GetUser(UserID);
            return User.ChangePassword(OldPassword, NewPassword);
        }

        public static string GetUsername(Guid userID)
        {
            try
            {
                MembershipUser User = Membership.GetUser(userID);
                return User.UserName;
            }
            catch
            {
                return null;
            }
        }

        public static bool UpdateCompany(Guid UserID, string Name,string Phone,string Address,string Logo)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                Company company = db.Companies.Where(c => c.Code.Equals(UserID)).FirstOrDefault();
                company.Name = Name;
                company.Phone = Phone;
                company.Address = Address;
                company.Logo = Logo;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

}